package com.github.freewu.commons.libs;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.github.freewu.commons.utils.ToastUtil;

/**
 * 下拉刷新和上拉刷新view
 * Created by free on 16-8-13.
 */
public class pullDownRefreshView extends ListView implements AbsListView.OnScrollListener, View.OnTouchListener {
    private int status=1;

    public final static int TOP = 1;
    public final static int BOTTOM = 2;
    public final static int NORMAL = 3;

    private int FirstTimeDistance =0;
    private int LastTimeDistance=0;

    private int SlideLevel=3;

    public pullDownRefreshView(Context context) {
        super(context);

        init();
    }

    public pullDownRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public pullDownRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {
        setOnScrollListener(this);
        setOnTouchListener(this);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            if (getLastVisiblePosition() == (getCount() - 1)) {
                ToastUtil.showShort(getContext(), "滑动到底部");

                status = BOTTOM;
            } else if (getFirstVisiblePosition() == 0) {
                ToastUtil.showShort(getContext(), "滑动到顶部");

                status = TOP;
            } else {
                status = NORMAL;
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                FirstTimeDistance =(int)event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int y = (int) event.getY();
                Log.i("MotionEventY", y + "px");

                if (status == TOP && FirstTimeDistance -y < 0) {
                    scrollTo(0, (FirstTimeDistance -y)/SlideLevel);

                    LastTimeDistance=y/SlideLevel;
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (LastTimeDistance>0){
                    SlideUp();
                    return true;
                }
                break;
        }
        return false;
    }

    public int getSlideLevel() {
        return SlideLevel;
    }

    public void setSlideLevel(int slideLevel) {
        if (slideLevel>0) {
            SlideLevel = slideLevel;
        }
    }

    /**
     * 滑动还原
     */
    private void SlideUp(){
        ValueAnimator animator=ValueAnimator.ofInt(LastTimeDistance-FirstTimeDistance/SlideLevel,0);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                scrollTo(0,-(int)animation.getAnimatedValue());
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                LastTimeDistance=0;
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setDuration(500);
        animator.start();
    }
}
